<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var arr = [4,6,2,9,3,5,6,14];
        var arr2 = [4,6,2,9,3,5,6,14];

        // 升序算法
        var a=arr.sort((a,b)=> {
            return a-b;
        })
        // 降序算法
        var b=arr2.sort((a,b)=> {
            return b-a;
        })
        console.log(...a);
        console.log(...arr);
        console.log(...b);
    </script>

<!-- 简单例子
    <script>
        var students =[
            {name:"zhang",age:"18",height:180},
            {name:"li",age:"19",height:170},
            {name:"wang",age:"14",height:200}
        ]
        console.log(...students);

        var newstudents = students.sort((a,b)=>{
            return a["age"]-b["age"];
            
        })
        console.log(...students);
    </script> -->

    
<!-- 封装示例 -->
    <!-- 封装示例 -->
    <script>
        var students =[
            {"name":"zhang","age":"18","height":130},
            {"name":"li","age":"19","height":170},
            {"name":"wang","age":"14","height":200}
        ]
        // 显示bug，实际此时上排序还未改变
        console.log(students);
        console.log(...students);
        
    
            // 升序
            function addOrder(arr,value){
                var res = arr.sort((a,b)=>{
                return a[value]-b[value];          
            })
                return res;
            }
            // 降速
            function rdcOrder(arr,value){
                var res = arr.sort((a,b)=>{
                return b[value]-a[value];          
            })
                return res;
            }
            // 按照age排序
            console.log(addOrder(students,"age"));
            console.log(students[0]);
    
            // 由这里可以知道，虽然显示结果有问题，实际上已经进行了排序。
            console.log(rdcOrder(students,"age"));
            console.log(students[0]);
            // 按照height排序
            console.log(...addOrder(students,"height"));
            console.log(...rdcOrder(students,"height"));
            console.log(...students);
</script>

</body>
</html>